package org.lc.stk.model.question.impl.fillBlank;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.annotations.Comment;
import org.lc.stk.model.question.Question;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.OrderBy;
import jakarta.persistence.Table;

/**
 * 填空题
 */
@Entity
@Table(name = "fill_blank_questions")
@Comment("填空题")
public class FillBlankQuestion extends Question {

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "stem_id")
    @Comment("题干")
    private FillBlankQuestionStem stem;

    @OneToMany(mappedBy = "question", cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy("blankIndex ASC")
    @Comment("答案列表")
    private List<FillBlankAnswer> answers = new ArrayList<>();

    @Column(name = "answers_interchangeable")
    @Comment("答案是否可互换")
    private boolean answersInterchangeable = false;

    public FillBlankQuestionStem getStem() {
        return stem;
    }

    public void setStem(FillBlankQuestionStem stem) {
        this.stem = stem;
    }

    /**
     * 设置题干内容，支持富文本格式
     * @param content HTML格式的题干，可包含公式、图片等富文本内容
     */
    public void setStemContent(String content) {
        if (this.stem == null) {
            this.stem = new FillBlankQuestionStem();
        }
        this.stem.setBody(content);
    }

    public String getStemContent() {
        return stem != null ? stem.getBody() : null;
    }

    public List<FillBlankAnswer> getAnswers() {
        return answers;
    }

    public void setAnswers(List<FillBlankAnswer> answers) {
        this.answers.clear();
        if (answers != null) {
            answers.forEach(this::addAnswer);
        }
    }

    public void addAnswer(FillBlankAnswer answer) {
        if (answer != null) {
            answer.setQuestion(this);
            this.answers.add(answer);
        }
    }

    /**
     * 添加填空答案
     * @param content HTML格式的答案内容，可包含公式、图片等富文本内容
     * @param blankIndex 填空位序号
     */
    public void addAnswer(String content, int blankIndex) {
        FillBlankAnswer answer = new FillBlankAnswer();
        answer.setBody(content);
        answer.setBlankIndex(blankIndex);
        addAnswer(answer);
    }

    public boolean isAnswersInterchangeable() {
        return answersInterchangeable;
    }

    public void setAnswersInterchangeable(boolean answersInterchangeable) {
        this.answersInterchangeable = answersInterchangeable;
    }

    /**
     * 验证题目格式和答案是否正确
     */
    public boolean validate() {
        if (stem == null || !stem.validate()) {
            return false;
        }

        if (answers == null || answers.isEmpty() || answers.size() != stem.getBlankCount()) {
            return false;
        }

        // 验证答案序号
        List<Integer> indexes = new ArrayList<>();
        for (FillBlankAnswer answer : answers) {
            if (answer.getBlankIndex() < 1 || answer.getBlankIndex() > stem.getBlankCount()) {
                return false;
            }
            if (indexes.contains(answer.getBlankIndex())) {
                return false;
            }
            indexes.add(answer.getBlankIndex());
        }

        return true;
    }
}